//
//  FitureChildrenContainerView.swift
//  taurus
//
//  Created by dyoung on 2022/1/29.
//  Copyright © 2022 com.fiture. All rights reserved.
//

import Foundation
import UIKit
import SnapKit

/// 模态弹出界面底部白色部分
class FitureChildrenContainerView: UIView {

    private var presentedView: UIView?
    private var naviView: UIView?
    private let topViewHeight: CGFloat = 72.0
    private let fitureCustomNavigationHeight = 60.0 /// 当前默认带导航栏的顶部高度为60.f
    private var config: FitureCustomPresentConfig = FitureCustomPresentConfig()
    var clikedDismiss: (() -> Void)?
    
    init(presentedView: UIView? = nil, naviView: UIView? = nil, config: FitureCustomPresentConfig) {
        self.presentedView = presentedView
        self.naviView = naviView
        self.config = config
        super.init(frame: .zero)
        _setupUI()
        _makeLayout()
        _updateUI()
        _bindUIEvent()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        let bezierPath = UIBezierPath(roundedRect: bounds,
                                      byRoundingCorners: [.topLeft, .topRight],
                                      cornerRadii: CGSize(width: 16.0, height: 16.0))
        let maskLayer = CAShapeLayer()
        maskLayer.frame = bounds
        maskLayer.path = bezierPath.cgPath
        layer.mask = maskLayer
    }
    
    private func _setupUI() {
        backgroundColor = .white
        addSubview(topIndicatorView)
        if let naviView = self.naviView {
            addSubview(naviView)
        }
        if let presentedView = self.presentedView {
            addSubview(closeButton)
            addSubview(titleLabel)
            addSubview(bottomLineView)
            addSubview(presentedView)
        }
    }
    
    private func _makeLayout() {
        topIndicatorView.snp.makeConstraints { m in
            m.centerX.equalToSuperview()
            m.top.equalToSuperview().offset(13)
            m.width.equalTo(18)
            m.height.equalTo(3)
        }
        if let naviView = self.naviView {
            naviView.snp.makeConstraints { m in
                m.top.equalToSuperview().offset(fitureCustomNavigationHeight - 44)
                m.width.bottom.equalToSuperview()
            }
        }
        if let presendtedView = self.presentedView {
            closeButton.snp.makeConstraints { m in
                m.left.equalToSuperview().offset(17)
                m.height.width.equalTo(30)
                m.top.equalToSuperview().offset(28)
            }
            titleLabel.snp.makeConstraints { m in
                m.centerX.equalToSuperview()
                m.centerY.equalTo(closeButton)
                m.top.equalToSuperview().offset(28)
                m.width.equalToSuperview().offset(-70 * 2)
            }
            bottomLineView.snp.makeConstraints { m in
                m.left.right.equalToSuperview()
                m.height.equalTo(0.5)
                m.bottom.equalToSuperview().offset(-0.5)
            }
            presendtedView.snp.makeConstraints { m in
                m.top.equalToSuperview().offset(topViewHeight)
                m.left.right.bottom.equalToSuperview()
            }
        }
    }
    
    private func _updateUI() {
        guard presentedView != nil else { return }
        titleLabel.text = config.title
        closeButton.isHidden = !config.needCloseBtn
        bottomLineView.isHidden = !config.needSeparaLine
    }
    
    private func _bindUIEvent() {
        closeButton.addTarget(self, action: #selector(clickCloseButton), for: .touchUpInside)
    }
    
    @objc func clickCloseButton() {
        clikedDismiss?()
    }
    
    // MARK: - lazy load
    private lazy var closeButton: UIButton = {
        let btn = UIButton(type: .custom)
        btn.frame = CGRect()
        btn.setImage(UIImage(named: "icon_nav_close")?.withRenderingMode(.alwaysOriginal), for: .normal)
        return btn
    }()

    private lazy var titleLabel: UILabel = {
        let title = UILabel()
        title.textAlignment = .center
        title.font = UIFont(name: "PingFangSC-Medium", size: 18) ?? UIFont.systemFont(ofSize: 18)
        title.textColor = .black
        title.isUserInteractionEnabled = false
        return title
    }()

    private lazy var topIndicatorView: UIView = {
        let view = UIView()
        view.backgroundColor = .gray
        view.layer.cornerRadius = 1.5
        view.layer.masksToBounds  = true
        view.isUserInteractionEnabled = false
        return view
    }()

    private lazy var bottomLineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.black
        view.alpha = 0.1
        view.isUserInteractionEnabled = false
        return view
    }()
    
}
